// pages/forum_content/forum_content.js
import { request } from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    forumId: '',
    user:'',
    content:'',//文本内容
    bottomHeight:0 //定义comment容器与page容器下边界之间的距离
  },

  // 通过forumId获取帖子内容
  getContent: function (e) {
    var that = this;
    var forumId = parseInt(this.data.forumId);
    console.log(forumId);
    wx.request({
      url: 'http://localhost:8080/forum/findbyid',
      method: 'POST',
      // header: {
      //   // "Content-Type": "application/x-www-form-urlencoded"
      //   "Content-Type": "application/json"
      // },
      data: forumId,
      success: function (res) {
        console.log(res.data);
        var message = res.data;
        if (message == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          console.log("已将论坛内容保存到message中");
          that.setData({
            message: res.data
          })
        }
      }
    })
  },

  // 通过forumId获取帖子对应的评论
  getReply: function (e) {
    var that = this;
    var forumId = parseInt(this.data.forumId);
    wx.request({
      url: 'http://localhost:8080/forum/reply',
      method: 'POST',
      header: {
        // "Content-Type": "application/x-www-form-urlencoded"
        "Content-Type": "application/json"
      },
      data: forumId,
      success: function (res) {
        console.log(res.data);
        var reply_content = res.data;
        if (reply_content == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          console.log("已将论坛内容保存到message中");
          that.setData({
            reply_content: res.data
          })
        }
      }
    })

  },


  // 
  gotoReply:function(e){
    wx.navigateTo({
      url: '/pages/forum/forum_release/forum_release'
    })
  },

  modify() {
    wx.navigateTo({
      url: '/pages/forum/forum_release/forum_release',
    })
  },

  // 评论功能
  bindfocus(e) {
    console.log(e, '键盘弹起')
    console.log(e)
    this.setData({
      bottomHeight: e.detail.height //将键盘的高度设置为comment容器与page容器下边界之间的距离。
    })

  },
  // 输入内容
  bindinput(e) {
    this.setData({
      content: e.detail.value
    })
    console.log(this.data.content)
  },
  // 失去焦点 
  bindblur(e) {
    console.log(e, '收起键盘')
    this.setData({
      bottomHeight: 0
    })
  },
  //
  sendOut() {
    let user = wx.getStorageSync('user')
    var forumId = parseInt(this.data.forumId);
    // console.log(user)
    var that = this
    that.setData({
      user: user
    })

    let data = {
      "forumid": forumId,
      "userid": that.data.user.id,
      "context": that.data.content
    }
    console.log(data)
    request({
      url: '/forum/addreply',
      method: "POST",
      data: data
    }).then(res => {
      if (res) {
        wx.showToast({
          title: '评论成功',
          icon: 'succcess'
        })
      } else {
        wx.showToast({
          title: '评论失败',
          icon: 'error'
        })
      }
    })
  },

  onLoad:function(options) {
    this.setData({
      forumId: options.id
    })
    this.getContent();
    this.getReply();
  },

})